﻿using System.Threading;
using System.Web.Http;
using Sustainalytics.Utils;
using Owin;

namespace Sustainalytics.PdfGeneration.Service
{
    using System.Web.Http.Dispatcher;

    using Sustainalytics.PdfGeneration.Service.Filters;
    using Sustainalytics.ReportGenerator.BL;

    public class Startup
    {
        public static readonly object LockerRunning = new object();

        public static readonly LogAggregator LogAggregator =
            new LogAggregator(new[] { PdfGenerationSerilog.Log, (ILogPdf)new PdfEmailLogger(), LogDebug.Log });

        public static bool IsGenerationRunning { get; set; }

        public static CancellationTokenSource Token { get; set; }

        public static TTLManager TtlManager { get; set; }

        public void Configuration(IAppBuilder builder)
        {
            var config = new HttpConfiguration();
            config.Filters.Add(new LogControllerFilter(true, true, false));
          
            var container = CompositionRoot.RegisterDependencies(config);
            config.Services.Replace(typeof(IHttpControllerActivator), new SimpleInjectorControllerActivator(container));

            WebApiConfig.Register(config);
            config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
            builder.UseWebApi(config);

            config.EnsureInitialized();
        }

        private static void SetFilters(HttpConfiguration config)
        {

            config.Filters.Add(new CustomExceptionFilterAttribute(LogAggregator));
        }
    }
}